package com.ky.facescan.api.rx;



import com.ky.facescan.api.exception.ApiException;
import com.ky.facescan.bean.base.BaseBean;
import com.ky.facescan.utils.LogUtils;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


public class RxHttpResponseCompat {

    //RxJava1写法
    public static <T> Observable.Transformer<BaseBean<T>, T> compatResult() {
        return new Observable.Transformer<BaseBean<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseBean<T>> baseBeanObservable) {
                return baseBeanObservable.flatMap((Func1<BaseBean<T>, Observable<T>>) tBaseBean -> {

                    if (tBaseBean.success()) {
                        return Observable.create((Observable.OnSubscribe<T>) subscriber -> {

                            // 不需要判断非空，因为登出就没有数据
                            subscriber.onNext(tBaseBean.getData());
                            subscriber.onCompleted();
                        });
                    } else {
                        String errorMsg = "";
                        if (tBaseBean.getMessage() == null) {
                            if (tBaseBean.getMsg() == null) {
                                errorMsg = "";
                            } else {
                                errorMsg = tBaseBean.getMsg();
                            }
                        } else {
                            errorMsg = tBaseBean.getMessage();
                        }
                        return Observable.error(new ApiException(tBaseBean.getCode(), errorMsg));
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
            }
        };
    }
    //RxJava1写法
    public static <T> Observable.Transformer<BaseBean<T>, T> compatResultAll() {
        return baseBeanObservable -> baseBeanObservable.flatMap((Func1<BaseBean<T>, Observable<T>>) tBaseBean -> {

            if (tBaseBean.success()) {
                LogUtils.e("RxHttpResponseCompat->success");
                return Observable.create((Observable.OnSubscribe<T>) subscriber -> {
                    LogUtils.e("RxHttpResponseCompat->onNext");
                    LogUtils.e("RxHttpResponseCompat->Code:" + tBaseBean.getCode());
                    LogUtils.e("RxHttpResponseCompat->Msg:" + tBaseBean.getMessage());
                    LogUtils.e("RxHttpResponseCompat->Data:" + tBaseBean.getData());
                    LogUtils.e("RxHttpResponseCompat->Success:" + tBaseBean.isSuccess());
                    // 不需要判断非空，因为登出就没有数据
                    subscriber.onNext(tBaseBean.getData());
                    subscriber.onCompleted();
                });
            } else {
                LogUtils.e("RxHttpResponseCompat->fail,请求成功，但是非正常结果");
                LogUtils.e("RxHttpResponseCompat->Code:" + tBaseBean.getCode());
                LogUtils.e("RxHttpResponseCompat->Msg:" + tBaseBean.getMessage());
                String errorMsg = "";
                if (tBaseBean.getMessage() == null) {
                    if (tBaseBean.getMsg() == null) {
                        errorMsg = "";
                    } else {
                        errorMsg = tBaseBean.getMsg();
                    }
                } else {
                    errorMsg = tBaseBean.getMessage();
                }
                return Observable.error(new ApiException(tBaseBean.getCode(), errorMsg));
            }

        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());


    }

//RxJava2写法
//    public static <T> ObservableTransformer<BaseBean<T>,T> compatResult(){
//
//
//        return  new ObservableTransformer<BaseBean<T>, T>() {
//            @Override
//            public ObservableSource<T> apply(Observable<BaseBean<T>> baseBeanObservable) {
//
//                return baseBeanObservable.flatMap(new Function<BaseBean<T>, ObservableSource<T>>() {
//                    @Override
//                    public ObservableSource<T> apply(@NonNull final BaseBean<T> tBaseBean) throws Exception {
//
//                        if(tBaseBean.success()){
//
//
//                            return Observable.create(new ObservableOnSubscribe<T>() {
//                                @Override
//                                public void subscribe(ObservableEmitter<T> subscriber) throws Exception {
//
//                                    try {
//                                        subscriber.onNext(tBaseBean.getData());
//                                        subscriber.onComplete();
//                                    }
//                                    catch (Exception e){
//                                        subscriber.onError(e);
//                                    }
//                                }
//                            });
//                        }
//                        else {
//                            return  Observable.error(new ApiException(tBaseBean.getStatus(),tBaseBean.getMessage()));
//                        }
//
//                    }
//                }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
//            }
//        };
//
//    }


}
